<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="openSheet"
    width="1000px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('currency')" prop="currency" :error="error.currency">
            <el-input v-model="form.currency" maxlength="3" :disabled="!createFlag" show-word-limit :placeholder="$t('inputCurrency')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('currencyName')" prop="currency_name" :error="error.currency_name">
            <el-input v-model="form.currency_name" maxlength="20" show-word-limit :placeholder="$t('inputCurrencyName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('currencyCountry')" prop="currency_country" :error="error.currency_country">
            <el-input v-model="form.currency_country" maxlength="30" show-word-limit :placeholder="$t('inputCurrencyCountry')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" @click="submitForm">{{ $t('create') }}</el-button>
      <el-button v-else type="primary" @click="submitForm">{{ $t('update') }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from '@/directive/el-drag-dialog'
import { createCurrencyApi, updateCurrencyApi } from '../api/currency.api';
export default {
  directives: { elDragDialog },
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      currencyId: undefined,
      menuOptions: [],
      form: {
        currency: undefined,
        currency_name: undefined,
        currency_country: undefined
      },
      error: {
        currency: undefined,
        currency_name: undefined,
        currency_country: undefined
      }
    };
  },
  computed: {
    rules() {
      return {
        currency: [
          { required: true, message: this.$t('currencyRequired'), trigger: 'blur' }]
      }
    }
  },
  methods: {
    async openSheetDialog(data) {
      this.openSheet = !this.openSheet;
      this.reset();
      if (data) {
        this.title = this.$t('currencyUpdateTitle');
        this.createFlag = false;
        this.currencyId = data.id;
        this.form = data;
      } else {
        this.title = this.$t('currencyCreateTitle');
        this.createFlag = true;
        this.currencyId = undefined;
      }
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form)
    },
    submitForm: function() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.resetJsonObject(this.error)
          var resp;
          if (this.createFlag) {
            resp = await createCurrencyApi(this.form);
          } else {
            resp = await updateCurrencyApi(this.currencyId, this.form);
          }
          if (resp.success) {
            this.$emit('refreshList');
            this.cancel();
            if (this.createFlag) {
              this.targaMessage(
                this.$t('createSuccess', {
                  title: this.$t('currency'),
                  value: this.form.currency
                })
              );
            } else {
              this.targaMessage(
                this.$t('updateSuccess', {
                  title: this.$t('currency'),
                  value: this.form.currency
                })
              );
            }
          } else {
            if (typeof resp.msg === 'object') {
              this.error = resp.msg;
            } else {
              this.targaMessageBox(this, resp.msg);
            }
          }
        }
      });
    }
  }
};
</script>

